package com.tcoiss.webservice.apiServer;

import com.tcoiss.common.core.enums.ErroEnums;
import com.tcoiss.common.core.exception.CustomException;
import com.tcoiss.common.core.utils.StringUtils;
import com.tcoiss.webservice.domain.ApiServiceConfig;
import com.tcoiss.webservice.utils.TemplateUtils;
import freemarker.template.Template;
import org.springframework.stereotype.Component;

@Component
public class InvokerBuilder {
    //private static Log logger=Logger.get(InvokerBuilder.class);


    public Invoker buildInvoker(ApiServiceConfig operation) {
        /*if(operation.isClosed()){
            return null;
        }*/
        String operationCode = operation.getApiCode();
        String endpoint = operation.getApiUrl();
        String appName = operation.getAppName();
        String requestTemplate = operation.getParamTemplate();
        String subRequestTemplate = operation.getSubParamTemplate();
        //String responseId = operation.getResponseId();
        if(StringUtils.isEmpty(endpoint)) {
            throw new CustomException(ErroEnums.APIERRO.getCode(),new Object[]{appName},
                    ErroEnums.APIERRO.getInfo() + "该操作["+operationCode+"]调用请求地址[" + endpoint +
                    "]未进行配置,请检查!");
        }
        //ClientRequest request = commBO.getRequest(requestId);
        if (StringUtils.isEmpty(requestTemplate)) {
            throw new CustomException(ErroEnums.APIERRO.getCode(),new Object[]{appName},
                    ErroEnums.APIERRO.getInfo() + "该操作["+operationCode+"]调用请求地址[" + endpoint +
                            "]未进行配置,请检查!");
        }
        /*ClientResponse response = commBO.getResponse(responseId);
        if (response == null) {
            throw new CodedException("", "该操作["+operationCode+"]调用反馈报文[" + responseId +
                    "]未进行配置,请检查!");
        }*/
        //ClientGlobalVar gvars = commBO.getGlobalVars(request.getGlobalVarsId());
        Invoker invoker = null;
        String epType = operation.getAppName();
        if("jingsui".equalsIgnoreCase(epType)){
            if("soap".equals(operation.getRequestType())){
                invoker = new SoapInvoker();
            }else {
                invoker = new HttpInvoker();
            }
        } else if ("k3cloud".equalsIgnoreCase(epType)) {
            invoker = new K3CloudInvoker();
        } else {
            invoker = new HttpInvoker();
        }
        invoker.endpoint = operation.getApiUrl();
        //invoker.timeout = operation.getTimeout();
        //invoker.transformFault = response.isTransformFault();
        //invoker.logColMap=commBO.getLogColsByOpId(operation.getOperationId());

        invoker.requestTemplate = buildRequestTemplate(requestTemplate);
        if(StringUtils.isNotBlank(subRequestTemplate)){
            invoker.subRequestTemplate = buildRequestTemplate(subRequestTemplate);
        }
        //地址模板
        invoker.urlTemplate = buildRequestTemplate(endpoint);
        //invoker.transformTemplate = buildTransformTemplate(response);

        return invoker;
    }

    /*private Template buildTransformTemplate(ClientResponse response) {
        if (!StringUtils.hasLength(response.getTransform())) {
            return null;
        }
        try {
            Template template = TemplateUtils.createTemplate(response.getTransform());
            return template;
        } catch (Exception e) {
            StringUtils.printInfo("buildTransformTemplate=="+response.getTransform());
            throw new CodedException("9999", "创建响应报文转换模板["+response.getResponseId()+"]出错.", e);
        }
    }*/
    private Template buildRequestTemplate(String requestTemplate) {
        try {
            Template template = TemplateUtils.createTemplate(requestTemplate);
            return template;
        } catch (Exception e) {
            //StringUtils.printInfo("buildRequestTemplate="+requestTemplate);
            throw new CustomException(ErroEnums.SYSERRO.getCode(),null, ErroEnums.SYSERRO.getInfo() + "创建请求报文模板["+requestTemplate+"]出错");
        }
    }
}
